﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.User.Entity;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.Web.User;

public partial class sysadmin_UserManager_AddTeacher :PowerCreator.LMS.Web.Base.BasePage
{
    User opUser = new User();
    Job opJob = new Job();
    Duty opDuty = new Duty();
    Department opDepartment = new Department();
    LogicServer.NetMeetingClass objNetMeetingClass;

    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!IsLicensed(PowerCreator.LMS.Action.Core.ActionInfo.AddTeacher))
        //{
        //    Script.RedirectURL(GetRefUrl());
        //}

        if (!IsPostBack)
        {
            //txt_UserName.BackColor = System.Drawing.Color.AntiqueWhite;
            //txt_UserPwd.BackColor = System.Drawing.Color.AntiqueWhite;
            //txt_RealName.BackColor = System.Drawing.Color.AntiqueWhite;
            InitPage();
            BinderDepartment();
           // BinderJob();
            //BinderDuty();
            if (PowerCreator.LMS.Web.Config.WebConfig.IsUseNetMeeting)
            {
                objNetMeetingClass = new LogicServer.NetMeetingClass();
            }
        }
    }

    //绑定部门类型信息
    private void BinderDepartment()
    {
        string search = "DepartmentType=1"; 
        DataTable dt = opDepartment.Query(search);
        ArrayList list = new ArrayList();
        list = opDepartment.GetDepartmentTree(dt);

        ddl_Department.DataSource = list;
        ddl_Department.DataTextField = "Text";
        ddl_Department.DataValueField = "Value";
        ddl_Department.DataBind();
    }
    //private void BinderJob()
    //{
    //    string search = "";
    //    DataTable dt = opJob.Query(search);
    //    ddl_Job.DataSource = dt;
    //    ddl_Job.DataTextField = "TeajobName";
    //    ddl_Job.DataValueField = "TeajobID";
    //    ddl_Job.DataBind();
    //}
    //private void BinderDuty()
    //{
    //    string search = "";
    //    DataTable dt = opDuty.Query(search);
    //    ddl_Duty.DataSource = dt;
    //    ddl_Duty.DataTextField = "TeadutyName";
    //    ddl_Duty.DataValueField = "TeadutyID";
    //    ddl_Duty.DataBind();
    //}

    protected override void InitPage()
    {
        string abc="";
        abc=GetLanageValueByKey("lbl_AddTeacher_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_InfoTitle_AddTeacher.Text = GetLanageValueByKey("lbl_AddTeacher_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Account_loading.Text = GetLanageValueByKey("lbl_AddTeacher_Account_loading", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_LoadingName.Text = GetLanageValueByKey("lbl_AddTeacher_LoadingName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_PassWord.Text = GetLanageValueByKey("lbl_AddTeacher_PassWord", PowerCreator.LMS.Language.LanagetItemType.Page);
        lblUserLoginIP.Text = GetLanageValueByKey("lbl_AddStudent_IsLoginIP", PowerCreator.LMS.Language.LanagetItemType.Page);

        lbl_AddTeacher_BaseInformaition.Text = GetLanageValueByKey("lbl_AddTeacher_BaseInformaition", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Name.Text = GetLanageValueByKey("lbl_AddTeacher_Name", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Number.Text = GetLanageValueByKey("lbl_AddTeacher_Number", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Sex.Text = GetLanageValueByKey("lbl_AddTeacher_Sex", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_IdNumber.Text = GetLanageValueByKey("lbl_AddTeacher_IdNumber", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Born.Text = GetLanageValueByKey("lbl_AddTeacher_Born", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Mail.Text = GetLanageValueByKey("lbl_AddTeacher_Mail", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Class.Text = GetLanageValueByKey("lbl_AddTeacher_Class", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Time.Text = GetLanageValueByKey("lbl_AddTeacher_Time", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_Job.Text = GetLanageValueByKey("lbl_AddTeacher_Job", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_Duty.Text = GetLanageValueByKey("lbl_AddTeacher_Duty", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Phone.Text = GetLanageValueByKey("lbl_AddTeacher_Phone", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Native.Text = GetLanageValueByKey("lbl_AddTeacher_Native", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Post.Text = GetLanageValueByKey("lbl_AddTeacher_Post", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_MobileTelephone.Text = GetLanageValueByKey("lbl_AddTeacher_MobileTelephone", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Address.Text = GetLanageValueByKey("lbl_AddTeacher_Address", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Remark.Text = GetLanageValueByKey("lbl_AddTeacher_Remark", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_School.Text = GetLanageValueByKey("lbl_AddTeacher_School", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_XueLi.Text = GetLanageValueByKey("lbl_AddTeacher_XueLi", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lab_Birth_css.Text = "(格式：1980-01-01)";
        //lab_JoinTime.Text = "(格式：1980-01-01)";
        radbtn_Male.Text = GetLanageValueByKey("EditUser_desc27", PowerCreator.LMS.Language.LanagetItemType.Page);
        radbtn_Female.Text = GetLanageValueByKey("EditUser_desc28", PowerCreator.LMS.Language.LanagetItemType.Page);
        rfv_txtUserName.ErrorMessage = GetLanageValueByKey("AddTeacher_msg_g1", PowerCreator.LMS.Language.LanagetItemType.Page);
        rfv_txtRealName.ErrorMessage = GetLanageValueByKey("AddTeacher_msg_g2", PowerCreator.LMS.Language.LanagetItemType.Page);
        ftv_txtUserPwd.ErrorMessage = GetLanageValueByKey("AddTeacher_msg_g4", PowerCreator.LMS.Language.LanagetItemType.Page);

        btn_AddNew.Text = GetLanageValueByKey("btn_AddNew", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Reset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }
    private bool CheckPageData()
    {
        if (txt_UserName.Text.Trim().Length < 5)
        {
            lbl_msg_Description.Text = GetLanageValueByKey("Login_username_msg1", PowerCreator.LMS.Language.LanagetItemType.Page);
            txt_UserName.BackColor = System.Drawing.Color.BlanchedAlmond;
            txt_UserName.Focus();
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
            return false;
        }
        if (txt_UserPwd.Text.Trim().Length < 6)
        {
            lbl_msg_Description.Text = GetLanageValueByKey("Login_username_msg2", PowerCreator.LMS.Language.LanagetItemType.Page);
            txt_UserPwd.BackColor = System.Drawing.Color.BlanchedAlmond;
            txt_UserPwd.Focus();
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
            return false;
        }

        string search = string.Format("UserName = '{0}'", txt_UserName.Text);
        if (opUser.Count(search) != 0)
        {
            lbl_msg_Description.Text = GetLanageValueByKey("theme_Register_Msg2", PowerCreator.LMS.Language.LanagetItemType.Page);
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
            return false;
        }
        if (!string.IsNullOrEmpty(txt_Birth.Text))
        {
            if (!TimeUtils.IsDateTime(txt_Birth.Text.Trim()))
            {
                lbl_msg_Description.Text = GetLanageValueByKey("EditorTeacher_msg_g1", PowerCreator.LMS.Language.LanagetItemType.Page);
                txt_Birth.BackColor = System.Drawing.Color.BlanchedAlmond;
                txt_Birth.Focus();
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
                return false;
            }
        }
        if (!string.IsNullOrEmpty(txt_JoinTime.Text))
        {
            if (!TimeUtils.IsDateTime(txt_JoinTime.Text.Trim()))
            {
                lbl_msg_Description.Text = GetLanageValueByKey("EditorTeacher_msg_g2", PowerCreator.LMS.Language.LanagetItemType.Page);
                txt_JoinTime.BackColor = System.Drawing.Color.BlanchedAlmond;
                txt_JoinTime.Focus();
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
                return false;
            }
        }
        if (!string.IsNullOrEmpty(txt_Email.Text))
        {
            if (!PageValidate.IsEmail(txt_Email.Text.Trim()))
            {
                lbl_msg_Description.Text = GetLanageValueByKey("EditorTeacher_msg_g3", PowerCreator.LMS.Language.LanagetItemType.Page);
                txt_Email.BackColor = System.Drawing.Color.BlanchedAlmond;
                txt_Email.Focus();
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
                return false;
            }
        }
        ////if (!string.IsNullOrEmpty(txt_Tel.Text))
        ////{
        ////    if (!PageValidate.IsPhone(txt_Phone.Text))
        ////    {
        ////        lbl_msg_Description.Text = GetLanageValueByKey("EditorTeacher_msg_g4", PowerCreator.LMS.Language.LanagetItemType.Page);
        ////        txt_Tel.BackColor = System.Drawing.Color.BlanchedAlmond;
        ////        txt_Tel.Focus();
        ////        Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
        ////        return false;
        ////    }
        ////}
        if (!string.IsNullOrEmpty(txt_Phone.Text))
        {
            if (!PageValidate.IsNumber(txt_Phone.Text))
            {
                lbl_msg_Description.Text = GetLanageValueByKey("EditorTeacher_msg_g5", PowerCreator.LMS.Language.LanagetItemType.Page);
                txt_Phone.BackColor = System.Drawing.Color.BlanchedAlmond;
                txt_Phone.Focus();
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
                return false;
            }
        }

        //if (!string.IsNullOrEmpty(txt_Tel.Text))
        //{
        //    if (!PageValidate.IsNumber(txt_Tel.Text))
        //    {
        //        lbl_msg_Description.Text = GetLanageValueByKey("EditorTeacher_msg_g6", PowerCreator.LMS.Language.LanagetItemType.Page);
        //        txt_Tel.BackColor = System.Drawing.Color.BlanchedAlmond;
        //        txt_Tel.Focus();
        //        Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
        //        return false;
        //    }
        //}
        return true;
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;
        VOTUser voUser = new VOTUser();
        voUser.UserName = txt_UserName.Text;
        voUser.UserPassword = txt_UserPwd.Text;
        voUser.UserLoginIP = txt_UserloginIP.Text;

        VOTTeacher voTeacher = new VOTTeacher();
        if(!string.IsNullOrEmpty(ddl_Department.SelectedValue.ToString()))
            voTeacher.FKTeacherDepartmentId = int.Parse(ddl_Department.SelectedValue.ToString());

        //if (!string.IsNullOrEmpty(ddl_Duty.SelectedValue.ToString()))
        //    voTeacher.FKTeacherTeadutyId = int.Parse(ddl_Duty.SelectedValue.ToString());
        //if (!string.IsNullOrEmpty(ddl_Job.SelectedValue.ToString()))
            //voTeacher.FKTeacherTeajobId = int.Parse(ddl_Job.SelectedValue.ToString());
        voTeacher.FKTeacherTeajobId = 0;
        voTeacher.FKTeacherTeadutyId = 0;

        voTeacher.TeacherAddress = txt_Address.Text;
        
        if(!string.IsNullOrEmpty(txt_Birth.Text))
            voTeacher.TeacherBirthday = DateTime.Parse(txt_Birth.Text);
        
        voTeacher.TeacherCode = txt_TeacherCode.Text;
        voTeacher.TeacherDescription = txt_Description.Text;
        voTeacher.TeacherEmail = txt_Email.Text;
        voTeacher.TeacherGraduateSchool = txt_SchoolName.Text;
        voTeacher.TeacherHomeplace = txt_Homeplace.Text;
        voTeacher.TeacherIdentityCard = txt_Idcard.Text;
        voTeacher.TeacherImageUrl = "";

        if(!string.IsNullOrEmpty(txt_JoinTime.Text))
            voTeacher.TeacherJoinTime = DateTime.Parse(txt_JoinTime.Text);
        voTeacher.TeacherMobileTelephone = txt_Phone.Text;
        voTeacher.TeacherName = txt_RealName.Text;
        voTeacher.TeacherPost = txt_Post.Text;
                          
        voTeacher.TeacherSex = 0;

        if(radbtn_Female.Checked)
        {
            voTeacher.TeacherSex = 1;
        }
        voTeacher.TeacherStudLevel = txt_XueLi.Text;
        voTeacher.TeacherTelephone = txt_Tel.Text;

        int UserID = 0;
        if (opUser.AddTeacher(voUser, voTeacher, GetCreatedByInfo(),ref UserID))
        {
            if (PowerCreator.LMS.Web.Config.WebConfig.IsUseNetMeeting)
            {
                objNetMeetingClass.WebAddUser(UserID, voUser.UserName, voUser.UserPassword, (ulong)voUser.UserRule, voUser.UserImageIndex, voTeacher.TeacherDescription, voTeacher.TeacherSex, voTeacher.TeacherTelephone, voUser.UserType);
            }
            string url = "TeacherManager.aspx";
            Script.RedirectURL(url);
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        string url = "TeacherManager.aspx";
        Script.RedirectURL(url);
    }
}
